package com.kaiyidiannao.activity.myJoin;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.kaiyidiannao.activity.tour.TourLineDetailActivity;
import com.kaiyidiannao.adapter.MyJoinAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.MyJoinBean;
import com.kaiyidiannao.model.req.model.PostMyJoinList;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by 武 on 2014/5/21 0021.
 */
public class MyJoinActivity extends BaseActionBarActivity implements Observer {
    private ActionBarView actionBarView;
    private DragListView listView;
    private int mid = 31;
    private View no_content_view;//没有数据的布局对象
    private ProgressBar loddingProgressBar;
    private String userid;
    private MyJoinAdapter adapter;
    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private ArrayList<MyJoinBean> myJoinBeans = DataCenter.myJoinBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_join_main);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        findActionBarView();
        initView();
        if (DataCenter.userLogin.size() != 0) {
            userid = DataCenter.userLogin.get(0).getUserid();
            new PostMyJoinList(userid, page).execute();
        }
        loddingProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onRestart() {
        if (DataCenter.userLogin.size() != 0) {
            userid = DataCenter.userLogin.get(0).getUserid();
            new PostMyJoinList(userid, page).execute();
        }
        loddingProgressBar.setVisibility(View.VISIBLE);
        super.onRestart();
    }

    private void findActionBarView() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.title.setText("我的加入");
        actionBarView.checkBox.setVisibility(View.GONE);
    }

    private void initView() {
        listView = (DragListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyJoinActivity.this, TourLineDetailActivity.class);
                intent.putExtra("itemid", myJoinBeans.get(i - 1).getItemid());
                intent.putExtra("catid", Integer.parseInt(myJoinBeans.get(i - 1).getCatid()));
                startActivity(intent);
                DataCenter.myjoin_position = i-1;
                DataCenter.PAGE = (i-1)/20+1;
                page = (i-1)/20+1;
                //page = i/10+1;
            }
        });

        listView.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                page = DataCenter.LISTVIEW_DEFAULT_PAGE;
                new PostMyJoinList(userid, page).execute();
            }

            @Override
            public void onLoadMore() {
                new PostMyJoinList(userid, ++page).execute();
            }
        });

        loddingProgressBar = (ProgressBar) findViewById(R.id.inner_progress_bar);
        no_content_view = findViewById(R.id.no_content_view);

        adapter = new MyJoinAdapter(this, myJoinBeans);
        listView.setAdapter(adapter);
    }

    @Override
    public void update(Observable observable, Object data) {
        if(!HttpUtils.isConnected()){
            listView.onRefreshComplete();
            listView.onLoadMoreComplete(false);
            //Toast.makeText(MyJoinActivity.this, "网络连接错误,请稍后重试", Toast.LENGTH_SHORT).show();
        }else{
            listView.onLoadMoreComplete(false);
            listView.onRefreshComplete();
        }

        DataCenter.flag flag = (DataCenter.flag) data;

        if (flag == DataCenter.flag.GET_MY_JOIN_SUCCESS) {
            if (myJoinBeans.size() <= 0) {
                showCurrentEmptyMsgView(getText(R.string.listview_msg_empty));
            } else {
//                adapter = new MyJoinAdapter(this, myJoinBeans);
//                listView.setAdapter(adapter);
                showCurrentDataView();
            }
        } else if (flag == DataCenter.flag.GET_MY_JOIN_FAILED) {
            if (myJoinBeans.size() <= 0) {
                showCurrentEmptyMsgView(getText(R.string.listview_msg_error));
            } else {
                showCurrentDataView();
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, MyJoinActivity.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

    /**
     * 显示列表数据
     */
    private void showCurrentDataView() {
        listView.setVisibility(View.VISIBLE);
        no_content_view.setVisibility(View.GONE);
        loddingProgressBar.setVisibility(View.GONE);
        adapter.notifyDataSetChanged();
    }

    /**
     * 正在加载，隐藏列表
     */
    private void showCurrentProgressView() {
        listView.setVisibility(View.GONE);
        no_content_view.setVisibility(View.GONE);
        loddingProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * 提示没有数据或者请求失败
     */
    private void showCurrentEmptyMsgView(CharSequence resStr) {
        listView.setVisibility(View.GONE);
        if (TextUtils.isEmpty(resStr)) {
            resStr = getText(R.string.listview_msg_empty);
        }
        ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(resStr);
        no_content_view.setVisibility(View.VISIBLE);
        loddingProgressBar.setVisibility(View.GONE);
    }
}
